version: "3.8"

services:

  nginx:
    image: nginx:1.25.3-bookworm-perl
    ports:
      - 80:80
    depends_on:
      app:
        condition: service_healthy
    healthcheck: 
      test: service nginx status || exit 1 
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    networks:
      - ephnet


  app:
    build:
      context: app
      dockerfile: dockerfile.app
    deploy:
      replicas: 3
    environment:
      POSTGRES_HOST: db
      POSTGRES_PORT: 5432
      POSTGRES_DBNAME: ephemerex
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    healthcheck:
      test: ["CMD", "curl", "http://localhost:8000/health"]
      interval: 10s
      timeout: 3s
      retries: 3
    ports:
      - "8000-8002:8000"
    depends_on:
      liquibase:
        condition: service_completed_successfully
    networks:
      - ephnet


  db:
    image: postgres:16
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "user"]
      timeout: 5s
      retries: 10
    environment:
      POSTGRES_DB: ephemerex
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    networks:
      - ephnet


  liquibase:
    image: liquibase/liquibase:4.5.0
    volumes:
      - ./app/liquibase:/liquibase/changelog
    working_dir: /liquibase/changelog
    command:
      [
        "liquibase",
        "--driver=org.postgresql.Driver",
        "--url=jdbc:postgresql://db:5432/ephemerex",
        "--changelog-file=changelog.yml",
        "--username=user",
        "--password=password",
        "--logLevel=warning",
        "--headless=true",
        "update"
      ]
    depends_on:
      db:
        condition: service_healthy
    networks:
      - ephnet


  swagger:
    image: swaggerapi/swagger-ui:v5.11.0
    ports:
      - 8080:8080
    volumes:
      - ./openapi.yml:/usr/share/nginx/html/openapi.yml
    environment:
      - API_URL=/openapi.yml
    depends_on:
      nginx:
        condition: service_healthy
    networks:
      - ephnet

  
  front:
    build:
      context: ./front
      dockerfile: dockerfile.front
    ports:
      - 1000:80
    depends_on:
      nginx:
        condition: service_healthy
    networks:
      - ephnet

networks:
  ephnet:
    driver: bridge
